package com.zijin.controller.unInterception;

import com.google.code.kaptcha.Producer;
import com.zijin.constant.CaptchaConstant;
import com.zijin.dto.user.UserSignUpDto;
import com.zijin.result.Result;
import com.zijin.service.user.IUserService;
import com.zijin.utils.PythonUtils;
import com.zijin.utils.SnowflakeIdGeneratorUtil;
import com.zijin.utils.ZiJinFileUtil;
import com.zijin.vo.CaptchaVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.data.redis.core.RedisTemplate;
import org.springframework.data.redis.core.ValueOperations;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.awt.image.BufferedImage;
import java.util.concurrent.TimeUnit;

/**
 * @Title: UnInterception
 * @Author JWei Xu
 * @Package com.zijin.controller.unInterception
 * @Date 2025/2/2 23:49
 * @description: 不拦截的相关类
 */

@RestController
@Api(tags = "不拦截的接口（不安全）")
@RequestMapping("/base/unSafe")
@Slf4j
public class UnInterception {

    @Resource
    IUserService userService;

    @Resource
    SnowflakeIdGeneratorUtil snowflakeIdGeneratorUtil;

    @Resource(name = "captchaProducerMath")
    private Producer captchaProducerMath;

    @Resource
    RedisTemplate redisTemplate;

    @GetMapping("YZMCode")
    @ApiOperation("验证码")
    public CaptchaVO YZMCode(){
        // 保存验证码信息
        long id = snowflakeIdGeneratorUtil.nextId();
        //将uuid放到了我们的密钥中+验证码 redis key(CAPTCHA_CODES::)作为密钥
        String verifyKey = CaptchaConstant.PREFIX_KEY + id;
        String capStr = null, code = null;//验证码
        BufferedImage image = null;//图片
        //生成验证码
        String capText = captchaProducerMath.createText();
        capStr = capText.substring(0, capText.lastIndexOf("@"));//8+1=？
        code = capText.substring(capText.lastIndexOf("@") + 1);//答案=9
        image = captchaProducerMath.createImage(capStr);
        String base64Img = ZiJinFileUtil.bufferedImagetoBase64(image, "jpg");
        ValueOperations valueOperations = redisTemplate.opsForValue();


        boolean flage = valueOperations.setIfAbsent(verifyKey,code,300, TimeUnit.SECONDS);//300秒过期
        while (!flage){
            //key重复，重新生成
            verifyKey = "CAPTCHA_CODE_KEY::" + id;
            flage = valueOperations.setIfAbsent(verifyKey,code,300, TimeUnit.SECONDS);//300秒过期
        }
        log.info("放入的key:{},放入的code:{},base64:{}",verifyKey,code,base64Img);

        base64Img = "data:image/jpg;base64,"+base64Img;


        CaptchaVO captchaVO = new CaptchaVO();
        captchaVO.setId(id);
        captchaVO.setImgBaseCode(base64Img);
        return captchaVO;
    }

    @GetMapping("/sendEmail")
    public Result sendEmail(String email){
        userService.sendEmail(email);
        return Result.success();
    }

    @GetMapping("/checkUser")
    @ApiOperation("检查用户名是否重复")
    public Result checkUser(String userName){
        userService.checkUser(userName);
        return Result.success();
    }

    @PostMapping("/registration")
    @ApiOperation("注册(自己注册)")
    public Result<Boolean> registration(@RequestBody UserSignUpDto dto){
        userService.registration(dto);
        return Result.success(true);
    }



}
